<?php
/* SVN FILE: $Id: ibMarket_1.x_to_2.x.php 225 2009-09-18 03:41:55Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* ibMarket Registry
* ibMarket common functions
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 225 $
* @lastrevision $Date: 2009-09-18 03:41:55 +0000 (Fri, 18 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-18 03:41:55 +0000 (Fri, 18 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/upgrade/ibMarket_1.x_to_2.x.php $
*/

define( 'IPB_THIS_SCRIPT', 'admin' );
if(file_exists('./initdata.php'))
{
	require_once( './initdata.php' );
}
elseif(file_exists('../initdata.php')) // Benefit of the doubt uploading the upgrade folder
{
	require_once( '../initdata.php' );
}
else
{
	die("You need to upload this script to the same directory with index.php and initdata.php and upgrade your board to IPB 3.0");
}

require_once( IPS_ROOT_PATH . 'sources/base/ipsRegistry.php' );

$registry   = ipsRegistry::instance();
$registry->init();
$DB         =  $registry->DB();
$settings   =& $registry->fetchSettings();

if($DB->checkForTable('market_versions') && !$DB->buildAndFetch(array('select'=>'*','from'=>'core_applications','where'=>"app_directory='ibmarket'")))
{
	require IPS_ROOT_PATH . "setup/sources/base/setup.php";
	
	$data      = IPSSetUp::fetchXmlAppInformation( 'ibmarket' );
	$_numbers  = IPSSetUp::fetchAppVersionNumbers( 'ibmarket' );
	$modules   = IPSSetUp::fetchXmlAppModules( 'ibmarket' );
	
	/* Grab Data */
	$data['app_directory']   = 'ibmarket';
	$data['current_version'] = ( $_numbers['current'][0] ) ? $_numbers['current'][0] : "None";
	$data['latest_version']  = $_numbers['latest'][1];
	$data['next_version']    = $_numbers['next'][0];
	
	$pos = $DB->buildAndFetch( array( 'select' => 'MAX(app_position) as pos', 'from' => 'core_applications' ) );
	$new_pos = intval( $pos['pos'] ) + 1;
	
	$first = $latest = NULL;
	$DB->build(array('select'=>"*",'from'=>"market_versions",'order'=>'version_id ASC'));
	$q = $DB->execute();
	while($version = $DB->fetch($q))
	{
		$latest = $version;
		if(is_null($first))
		{
			$first = $version;
		}
		$DB->insert( 'upgrade_history', array( 
													'upgrade_version_id'	=> $version['version_id'],
													'upgrade_version_human'	=> $version['version_name'],
													'upgrade_date'			=> $version['up_time'],
													'upgrade_mid'			=> 0,
													'upgrade_app'			=> 'ibmarket'
											)	);
	}
	
	$DB->insert('core_applications',array(
											'app_title'			=> $data['name'],
											'app_public_title'	=> $data['public_name'],
											'app_author'		=> $data['author'],
											'app_description'	=> $data['description'],
											'app_hide_tab'		=> intval($data['hide_tab']),
											'app_version'		=> $latest['version_name'],
											'app_long_version'	=> $latest['version_id'],
											'app_directory'		=> 'ibmarket',
											'app_location'		=> 'other',
											'app_added'			=> $first['up_time'],
											'app_position'		=> $new_pos,
											'app_protected'		=> 0,
											'app_enabled'		=> $data['disabledatinstall'] ? 0 : 1
				)						);
	
	$DB->dropTable("market_versions");
}

if(@unlink(__FILE__))
{
	echo "The file have been successfully removed.";
}
else
{
	echo "Please remove this script.";
}
$upgradeUrl = $settings['board_url'] . '/' . CP_DIRECTORY . '/upgrade/index.php';
echo "\n<br />\n";
echo "<a href='{$upgradeUrl}'>Go to IP.Board upgrade</a>";
exit();